package ar.edu.unicen.exa.isistan.archassis.view.web;

import org.apache.velocity.VelocityContext;

import ar.edu.unicen.exa.isistan.archassis.ia.api.EvalResult;
import ar.edu.unicen.exa.isistan.archassis.model.ModelManager;
import ar.edu.unicen.exa.isistan.archassis.rcs.IRevisionControlSystem;
import ar.edu.unicen.exa.isistan.archassis.rcs.svn.SVNRevisionControlSystem;
import ar.edu.unicen.exa.isistan.archassis.source.FileSourceStrategy;
import ar.edu.unicen.exa.isistan.archassis.source.ISourceStrategy;
import ar.edu.unicen.exa.isistan.archassis.source.RCSSourceStrategy;


public class SummaryVelocityThread extends AbstractVelocityThread {

	
	public SummaryVelocityThread(EvalResult evalResult) {
		super(evalResult);
	}

	protected String getFileName() {
		return "summary.html";
	}

	@Override
	protected VelocityContext createContext(EvalResult evalResult)
			throws Exception {
		VelocityContext context = new VelocityContext();
		context.put("analisis", evalResult);
		context.put("resultados", evalResult.getJavaClassCollector().getResult());
		context.put("sizeCumplido", evalResult.getJavaClassCollector().getResult().size());
		context.put("sizeSobrante", evalResult.getJavaClassCollector().getResultNoCumple().size());
		context.put("casos", evalResult.getCaseSummaryDataList());
		//cantidad de clases procesadas
		//cantidad de casos en la DB
		
		context.put("url", getURL());
		context.put("summary", evalResult.getSummaryAmountData());
//		context.put("sizeSobrante", evalResult.getResultNoCumple().size());
//		context.put("casos", evalResult.getCaseByClass());
		return context;
	}
	
	private String getURL() {
		ISourceStrategy sourceStrategy = ModelManager.getInstance().getSourceStrategy();
		if (sourceStrategy instanceof FileSourceStrategy) {
			FileSourceStrategy fileSourceStrategy = (FileSourceStrategy) sourceStrategy;
			return fileSourceStrategy.getPath();
		} else if (sourceStrategy instanceof RCSSourceStrategy) {
			RCSSourceStrategy rcsSourceStrategy = (RCSSourceStrategy) sourceStrategy;
			StringBuffer result = new StringBuffer(getPath(rcsSourceStrategy.getRcs()));
			result.append(" [");
			result.append(rcsSourceStrategy.getVersionFrom().getID());
			result.append(" - ");
			result.append(rcsSourceStrategy.getVersionTo().getID());
			result.append("]");
			return result.toString();
		}
		return "??";
		
	}
	private String getPath(IRevisionControlSystem rcs) {
		if (rcs instanceof SVNRevisionControlSystem) {
			SVNRevisionControlSystem svnRCS = (SVNRevisionControlSystem) rcs;
			return svnRCS.getUserSVNURL();
		}
		return "";
	}

	@Override
	protected String getTemplateName() {
		return SummaryExport.SUMMARY_TEMPLATE;
	}
}
